package com.yunmang.activity.setting;

import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.bigkoo.alertview.AlertView;
import com.bigkoo.alertview.OnItemClickListener;
import com.yunmang.R;
import com.yunmang.activity.BaseActivity;
import com.yunmang.app.AppConstant;
import com.yunmang.entity.NameBean;
import com.yunmang.http.HttpUtils;
import com.yunmang.utils.CommonUtils;
import com.yunmang.utils.DeviceUtils;
import com.yunmang.utils.SharedPreferencesUtils;

import java.util.HashMap;
import java.util.Map;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import rx.Subscriber;

/**
 * Created by Administrator on 2017/5/8.
 */
public class SetingPwdActivity extends BaseActivity implements OnItemClickListener {
    @InjectView(R.id.et_old)
    EditText etOld;
    @InjectView(R.id.et_newpwd)
    EditText etNewpwd;
    @InjectView(R.id.line_phone)
    View linePhone;
    @InjectView(R.id.et_secondpwd)
    EditText etSecondpwd;
    @InjectView(R.id.pwd_line)
    View pwdLine;
    @InjectView(R.id.confirm)
    Button confirm;
    @InjectView(R.id.toolbar)
    Toolbar mToolbar;
    private Subscriber<NameBean> subscriber;

    @Override
    public int getLayoutId() {
        return R.layout.activity_set_pwd;
    }

    @Override
    public void initView() {
        ButterKnife.inject(this);
        initToolbar();
    }

    @Override
    public void initData() {

    }

    private void changePwd(String oldPwd, final String newPwd) {
        Map<String, String> params = new HashMap<>();
        params.put("userName", AppConstant.username);
        params.put("oldPassword", oldPwd);
        params.put("newPassword", newPwd);
        subscriber = new Subscriber<NameBean>() {
            @Override
            public void onCompleted() {
                hideWaitingDialog();
            }

            @Override
            public void onError(Throwable e) {
                e.printStackTrace();
                showToast("修改失败");
            }

            @Override
            public void onNext(NameBean nameBean) {
                if(nameBean.getCode()==200){
                    showToast("修改成功");
                    SharedPreferencesUtils.saveParameter(context,"password",newPwd);
                    etNewpwd.setText("");
                    etOld.setText("");
                    etSecondpwd.setText("");
                }else if(nameBean.getCode()==300){
                    showToast("登录密码不正确");
                }else{
                    showToast("修改失败");
                }
            }
        };
        HttpUtils.getInstance().changePwd(subscriber, params);
    }

    private void initToolbar() {
        setSupportActionBar(mToolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setTitle("密码修改");
        mToolbar.setNavigationIcon(R.mipmap.ic_back);
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    @OnClick(R.id.confirm)
    public void onClick() {
        if(!DeviceUtils.isHasNetWork(this)){//请检查网络设置
            new AlertView("网络异常", "网络不可用，请检查您的网络环境设置", null, new String[]{"确定"}, null, this, AlertView.Style.Alert, this).show();
            return;
        }else{
            String oldPwd= CommonUtils.getTextViewValue(etOld);
            String newPwd=CommonUtils.getTextViewValue(etNewpwd);
            String secondPwd=CommonUtils.getTextViewValue(etSecondpwd);
            if(TextUtils.isEmpty(oldPwd)){
                showToast("请输入旧密码");
                return;
            }
            if(TextUtils.isEmpty(newPwd)){
                showToast("请输入新密码");
                return;
            }
            if(TextUtils.isEmpty(secondPwd)){
                showToast("请输入确认密码");
                return;
            }
            if(!newPwd.equals(secondPwd)){
                showToast("两次输入的密码不一致，请重新输入");
                return;
            }
            showWaitingDialog("正在上传……");
            changePwd(oldPwd,newPwd);
        }
    }

    @Override
    public void onItemClick(Object o, int position) {

    }
}
